package com.mayuanfei.service.impl;

import cn.hutool.extra.spring.SpringUtil;
import com.aizuda.snailjob.client.core.annotation.Propagation;
import com.aizuda.snailjob.client.core.annotation.Retryable;
import com.aizuda.snailjob.client.core.retryer.RetryType;
import com.mayuanfei.common.constant.SceneConstant;
import com.mayuanfei.common.exception.BizException;
import com.mayuanfei.pojo.OrderBo;
import com.mayuanfei.service.RemoteRetryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * TODO 类描述
 *
 * @author 老马
 * @date 2025-04-07 13:50
 */
@Slf4j
@Service
public class RemoteRetryServiceImpl implements RemoteRetryService {
    @Override
    @Retryable(scene = SceneConstant.REMOTE_RETRY,
            retryStrategy = RetryType.ONLY_REMOTE,
            idempotentId = MyIdempotentId.class)
    public void remoteRetry(String params) {
        System.out.println("remote retry 方法开始执行");
        throw new RuntimeException("remote retry 方法执行异常");
    }

    @Override
    @Retryable(scene = "remoteRetryMyIdempotentIdGenerate",
            retryStrategy = RetryType.ONLY_REMOTE,
            idempotentId = MyIdempotentId.class)
    public void remoteRetryMyIdempotentIdGenerate(String params) {
        System.out.println("remote retry MyIdempotentId 方法开始执行");
        throw new RuntimeException("remote retry MyIdempotentId 方法执行异常");
    }

    @Override
    @Retryable(scene = SceneConstant.REMOTE_RETRY + "2"
            , retryStrategy = RetryType.ONLY_REMOTE
            )
    public void remoteRetryNoException(String params) {
        System.out.println("测试异常方法开始执行");
        throw new BizException("10001", "测试异常方法执行异常");
    }

    @Override
    @Retryable(scene = SceneConstant.REMOTE_RETRY + "3"
            , retryStrategy = RetryType.ONLY_REMOTE
            , include = {BizException.class})
    public void remoteRetryIncludeException(String params) {
        System.out.println("测试include异常方法开始执行");
        throw new RuntimeException("测试include异常方法开始执行");
    }

    @Override
    @Retryable(scene = SceneConstant.REMOTE_RETRY + "4"
            , retryStrategy = RetryType.ONLY_REMOTE
            , exclude = {BizException.class})
    public void remoteRetryExcludeException(String params) {
        System.out.println("测试exclude异常方法开始执行");
        throw new BizException("10001", "测试exclude异常方法开始执行");
    }

    @Retryable(scene = "remoteRetryPropagation",
            retryStrategy = RetryType.ONLY_REMOTE)
    @Override
    public void remoteRetryPropagation(String params) {
        System.out.println("remote retry propagation 方法开始执行");
        // 调用A方法
        RemoteRetryServiceImpl agentThisClass = SpringUtil.getBean(RemoteRetryServiceImpl.class);
        agentThisClass.remoteRetryPropagationA(params);
    }

    @Retryable(scene = "remoteRetryPropagationA",
            retryStrategy = RetryType.ONLY_REMOTE,
            propagation = Propagation.REQUIRES_NEW)
    public void remoteRetryPropagationA(String params) {
        System.out.println("remote retry propagation A");
        // 调用B方法
        RemoteRetryServiceImpl agentThisClass = SpringUtil.getBean(RemoteRetryServiceImpl.class);
        agentThisClass.remoteRetryPropagationB(params);
    }

    @Retryable(scene = "remoteRetryPropagationB",
            retryStrategy = RetryType.ONLY_REMOTE,
            propagation = Propagation.REQUIRED,
            isThrowException = false)
    public void remoteRetryPropagationB(String params) {
        System.out.println("remote retry propagation B");
        throw new RuntimeException("remote retry propagation B");
    }

    @Retryable(scene = "retryCompleteCallback",
            retryStrategy = RetryType.ONLY_REMOTE,
            retryCompleteCallback = MyRetryCompleteCallback.class)
    @Override
    public void remoteRetryCompleteCallback(String params) {
        System.out.println("remote retry complete callback 方法开始执行");
        throw new RuntimeException("remote retry complete callback 方法执行异常");
    }

    @Override
    @Retryable(scene = "remoteRetryBizNo",
            retryStrategy = RetryType.ONLY_REMOTE,
            bizNo = "#request.orderNo")
    public void remoteRetryBizNo(OrderBo request) {
        System.out.println("remote retry BizNo 方法开始执行");
        throw new RuntimeException("remote retry BizNo 方法执行异常");
    }


}